<%@ taglib prefix="s" uri="/struts-tags"%>
<%@ taglib uri="http://displaytag.sf.net" prefix="display"%>
<%@ taglib prefix="sx" uri="/struts-dojo-tags" %>

<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<%@ taglib uri="/WEB-INF/struts-tiles.tld" prefix="tiles" %>
<%@ taglib uri="/WEB-INF/vectrics.tld"    prefix="vectrics" %>

<%@ page import="org.vectrics.hibernate.ThreadProperties" %>
<%@ page import="org.vectrics.domain.Domain" %>
<%@ page import="java.util.Collection" %>
<%@ page import="org.vectrics.recruiter.RecruitServiceLocator" %>

<%@page import="org.vectrics.cv.Skill"%>
<%@page import="org.vectrics.cv.Level"%>
<%@page import="org.vectrics.provider.CoreServiceLocator"%>
<%@page import="java.util.Iterator"%>
<%@page import="org.apache.commons.collections.Predicate"%>
<%@page import="org.apache.commons.collections.CollectionUtils"%>
<%@page import="org.vectrics.cv.PersonSkill"%>
<%@page import="org.vectrics.user.Person"%>
<%@page import="java.util.ArrayList"%>

<head>
	<sx:head cache="false" />
	<link rel="stylesheet" href="/hrs-intranet/styles/displaytag.css" type="text/css" />
	<link rel="stylesheet" href="/hrs-intranet/styles/errortag.css" type="text/css" />
	
	<link rel="stylesheet" type="text/css" href="/hrs-intranet/js/dynamicTree/DynamicTree.css" />
	<script language="JavaScript" type="text/javascript" src="/hrs-intranet/js/dynamicTree/DynamicTree.js"></script>
	<script language="JavaScript" type="text/javascript" src="/hrs-intranet/js/dynamicTree/ie5.js"></script>
</head>

<%
	// Recupero la lista de todos los skills padres
	Collection<Skill> parentSkills = CoreServiceLocator.getPersonService().findAllParentSkill();
	
	// Recupero los levels
	Collection<Level> levels = CoreServiceLocator.getPersonService().findAllLevels();
	
    //seteo la request 
	//request.setAttribute("parentSkills", parentSkills);
	//request.setAttribute("levels", levels);
	
%>

<jsp:useBean 	id="skillForm" 
				class="org.vectrics.recruiter.struts.cv.SkillForm" 
				scope="request"/>

	<h3>Reporte de Candidatos</h3>
	
<s:if test="hasFieldErrors()">
	<div class="oldErrorMsg">
		<h4 class="oldErrorTitle">Error(s) with submission:</h4>
		<s:iterator value="fieldErrors">
			<s:iterator value="value">
				<li>
					<font class="errorMsgFont">
						<s:property/>
					</font>
				</li>
			</s:iterator>
		</s:iterator>
	</div>
	<br/>
	<br/>
</s:if>

<s:form cssClass="formClass">

<table width="400">
		<tr>
			<td valign="middle" >
					<div class="DynamicTree">
						<div class="wrap" id="tree">
	
	<%
		// recorremos los skills padres - SKP1
		// Los skill se cargan solo en 3 niveles como maximo.
		for (Skill parentSkill : parentSkills) {
			
			// IF
			if ((parentSkill.getChildSkills() != null) && !parentSkill.getChildSkills().isEmpty()) {
	%>
				<div class="folder">
						<input type="checkbox" name="padre" value="" disabled="disabled" /><%= parentSkill.getName() %>
	
	<%
				// recorremos los skills hijos - SKC1
				for (Skill skill : parentSkill.getChildSkills()) {
					// si no tiene hijos lo dibujo
					if ((skill.getChildSkills() == null) || skill.getChildSkills().isEmpty()) {
						// buscamos si el skill hijo lo tiene la persona para poder checkearlo o no
						final Long idSkill = skill.getId();
						Predicate preSkill = new Predicate() {
			                public boolean evaluate(Object objeto) {
			                	PersonSkill skillPre = (PersonSkill) objeto;
			                    return (skillPre.getSkill().getId().equals(idSkill));
			                }
			            };
	%>
										<div class="doc">
												<input type="checkbox" name="skill_<%= skill.getId() %>" value="<%= skill.getId() %>" /><%= skill.getName() %>
												<input type="hidden" id="l_<%=skill.getId() %>" value="0" />
										</div>
	<%					
					} else {
						//Si tiene hijos lo muestro y muestro sus hijos
					%>
						<div class="folder">
						<input type="checkbox" name="padre" value="" disabled="disabled" /><%= skill.getName() %>
                   <% 
						// recorremos los skills hijos - SKC1
						for (Skill child : skill.getChildSkills()) {
							// buscamos si el skill hijo lo tiene la persona para poder checkearlo o no
							final Long idChild = child.getId();
							Predicate preChild = new Predicate() {
					            public boolean evaluate(Object objeto) {
					               	PersonSkill skillPre = (PersonSkill) objeto;
					                return (skillPre.getSkill().getId().equals(idChild));
					            }
					        };
			       %>
							    <div class="doc">
												<input type="checkbox" name="skill_<%= child.getId() %>" value="<%= child.getId() %>" /><%= child.getName() %>
												<input type="hidden" id="l_<%=child.getId() %>" value="0" />
								</div>
			<%	
					}
                  %>  </div> <% 
				}
			  } // END SKC1
	%>
									</div>
	<%
		
			}	// END IF
		} // END SKP1
	%>
						</div>
					</div>
					
			</td>
		</tr>
	</table>
	
			 <s:hidden key="reportName" value="Candidate List Report"/>
		 	<s:submit action="CandidateListReportAction" method="exportToPDF" value="Export To PDF"  cssClass="btnLarge" cssStyle="position:relative; left:-855; top:-3px;"/>
		 	<s:submit action="CandidateListReportAction" method="exportToExcel" value="Export To Excel" cssClass="btnLarge" cssStyle="position:relative; left:-855; top:0px;"/>

	</s:form>
	
	<div style="display: none;" id="divLevel">
	<select name="level" id="level">
	<%
		// recorremos y armamos los options del combo level
		for (Level level : levels) {
	%>
		<option value="<%= level.getId() %>"><%= level.getName() %></option>
	<%
		}
	%>
	</select>
</div>
<script type="text/javascript">
	var tree = new DynamicTree("tree");
	tree.init();
</script>
